package com.gzx.plugin.data.wckj.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.gzx.plugin.data.wckj.api.ApiResponse;
import com.gzx.plugin.data.wckj.api.CaseResult;
import com.gzx.plugin.data.wckj.entity.*;
import com.gzx.plugin.data.wckj.mapper.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
@Slf4j
@DS("data")
@RequiredArgsConstructor
public class CaseInfoBatchSaveService {

    private final DataWckjYwyhidMapper ywyhidDao;
    private final WckjJudgeTransferInfoDao wckjJudgeTransferInfoDao;
    private final WckjJudgeRepayPlanInfoDao wckjJudgeRepayPlanInfoDao;
    private final WckjJudgeRepayInfoDao wckjJudgeRepayInfoDao;
    private final WckjJudgeLoanInfoDao wckjJudgeLoanInfoDao;
    private final WckjJudgeUserInfoDao wckjJudgeUserInfoDao;

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE, timeout = 10, rollbackFor = Exception.class)
    public void saveBatchRequestData(String projectCode, ConcurrentHashMap<String, ApiResponse> resultMap) {
        ArrayList<JudgeTransferInfo> transferBatchList = new ArrayList<>();
        ArrayList<JudgeRepayPlanInfo> replayPlanBatchList = new ArrayList<>();
        ArrayList<JudgeRepayInfo> repayBatchList = new ArrayList<>();
        ArrayList<JudgeLoanInfo> loanBatchList = new ArrayList<>();
        ArrayList<JudgeUserInfo> userInfoList = new ArrayList<>();

        for (Map.Entry<String, ApiResponse> entry : resultMap.entrySet()) {
            ApiResponse apiResponse = entry.getValue();
            if (!CollectionUtils.isEmpty(apiResponse.getTransfer())) {
                transferBatchList.addAll(apiResponse.getTransfer());
            }
            if (!CollectionUtils.isEmpty(apiResponse.getReplayPlan())) {
                replayPlanBatchList.addAll(apiResponse.getReplayPlan());
            }
            if (!CollectionUtils.isEmpty(apiResponse.getRepay())) {
                repayBatchList.addAll(apiResponse.getRepay());
            }
            if (!CollectionUtils.isEmpty(apiResponse.getLoan())) {
                loanBatchList.addAll(apiResponse.getLoan());
            }
            if (!CollectionUtils.isEmpty(apiResponse.getUserInfo())) {
                userInfoList.addAll(apiResponse.getUserInfo());
            }
        }

        if (!CollectionUtils.isEmpty(userInfoList)) {
            log.info("loanBatchList 中的条数{}", userInfoList.size());
            wckjJudgeUserInfoDao.batchSave(userInfoList);
        }
        if (!CollectionUtils.isEmpty(transferBatchList)) {
            log.info("transferBatchList中的条数{}", transferBatchList.size());
            wckjJudgeTransferInfoDao.batchSave(transferBatchList);
        }
        //批量保存-还款计划列表
        if (!CollectionUtils.isEmpty(replayPlanBatchList)) {
            log.info("replayPlanBatchList 中的条数{}", replayPlanBatchList.size());
            wckjJudgeRepayPlanInfoDao.batchSave(replayPlanBatchList);
        }
        //批量保存-还款信息列表
        if (!CollectionUtils.isEmpty(repayBatchList)) {
            log.info("repayBatchList 中的条数{}", repayBatchList.size());
            wckjJudgeRepayInfoDao.batchSave(repayBatchList);
        }
        //批量保存-代偿信息列表
        if (!CollectionUtils.isEmpty(loanBatchList)) {
            log.info("loanBatchList 中的条数{}", loanBatchList.size());
            wckjJudgeLoanInfoDao.batchSave(loanBatchList);
        }
        //更新业务用户数据状态从0变成1，代表已处理
        ArrayList<String> userIdBatchList = new ArrayList<>(resultMap.keySet());
        ywyhidDao.updateDataBatch(userIdBatchList);
        if (CollectionUtils.isEmpty(CaseResult.map.get(projectCode))) {
            CaseResult.map.put(projectCode, new ArrayList<>());
        }
        CaseResult.map.get(projectCode).addAll(resultMap.values());
        log.info("本次处理uid共{}条, update uid 为:{}", userIdBatchList.size(), JSON.toJSONString(userIdBatchList));
    }
}
